﻿using BLL;
using DBUtility;
using EncryptHelper;
using gfLibrary;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal
{
    public partial class Update : System.Web.UI.Page
    {
        private int CurrentId
        {
            get
            {
                string q= HttpContext.Current.Request.QueryString["ID"] == null ? "0" : Request.QueryString["ID"];
                int qid = int.Parse(q);
                return qid;
            }
        }
        private string Pass { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetModel();
            }
        }

        protected void GetModel()
        {
            CONST_DEFINE_BLL bll = new CONST_DEFINE_BLL();
            var role = bll.GetModelList("CON_CHNAME='ZNYHGW'");
            ListItem items;
            foreach (var r in role)
            {
                items = new ListItem();
                items.Text = r.CON_CON;
                items.Value = r.CON_CODE;
                chbELicense.Items.Add(items);
            }

            if (CurrentId > 0)
            {
                txtTitle.Text = "★修改页面";
                EMPLOYEE_USER_BLL user = new EMPLOYEE_USER_BLL();
                EMPLOYEE_USER theone = user.GetModel(CurrentId);
                txtENo.Text = theone.EMPLOYEE_NO;
                txtEId.Text = theone.EMPLOYEE_ID;
                //登陆名无法修改
                txtEId.Enabled = false;
                txtEName.Text = theone.EMPLOYEE_NAME;
                Pass = theone.EMPLOYEE_PWD;
                int item;
                string power = theone.EMPLOYEE_POWER;

                if (power.Length == 1)
                {
                    if (int.TryParse(power, out item))
                    {
                        drpEPower.SelectedIndex = item;
                    }
                    else
                    {
                        drpEPower.SelectedIndex = 6;
                    }
                }
                else
                {
                    string index = Encrypt.AESDecrypt(theone.EMPLOYEE_POWER);
                    if (int.TryParse(index, out item))
                    {
                        drpEPower.SelectedIndex = item;
                    }
                    else
                    {
                        drpEPower.SelectedIndex = 6;
                    }
                }


                string str = theone.LICENSE_NAME;
                for (int i = 0; i < str.Split(',').Length; i++)
                {
                    for (int j = 0; j < chbELicense.Items.Count; j++)
                    {
                        if (str.Split(',')[i] == chbELicense.Items[j].Value)
                        {
                            chbELicense.Items[j].Selected = true;
                        }
                    }
                }
            }
        }
        

        protected void btnSaveImg_Click(object sender, EventArgs e)
        {
            string strNo = txtENo.Text.Trim();
            if (string.IsNullOrEmpty(strNo))
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "员工编号不能为空");
                txtENo.Focus();
                return;
            }

            string strId = txtEId.Text.Trim();
            if (string.IsNullOrEmpty(strId))
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "员工账户不能为空");
                txtEId.Focus();
                return;
            }

            string strName = txtEName.Text.Trim();
            if (strName.Trim().Length == 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "员工姓名不能为空");
                txtEName.Focus();
                return;
            }

            DbFactory m_df = new DbFactory();

            EMPLOYEE_USER_BLL user = new EMPLOYEE_USER_BLL();
            EMPLOYEE_USER theone = new EMPLOYEE_USER();
            theone.EMPLOYEE_NO = txtENo.Text;
            theone.EMPLOYEE_ID = txtEId.Text;
            theone.EMPLOYEE_NAME = txtEName.Text;
            theone.LICENSE_ID = txtEId.Text;

            if (rbtUpdate.Checked)
            {
                theone.EMPLOYEE_PWD = Encrypt.MD5Encrypt(txtEId.Text.Trim().ToLower() + txtEPwd.Text.Trim().ToLower());
            }
            if (rbtReset.Checked)
            {
                string resetPWD = "123456";
                theone.EMPLOYEE_PWD = Encrypt.MD5Encrypt(txtEId.Text.Trim().ToLower() + resetPWD);
            }

            //POWER

            theone.EMPLOYEE_POWER = drpEPower.SelectedItem.Value;
      
            //REG_DATE
            theone.REG_DATE = DateTime.Now.ToString();
            //LICENSE
            string selectValue = "";
            int mark = 0;
            for (int i = 0; i < chbELicense.Items.Count; i++)
            {
                if (chbELicense.Items[i].Selected)
                {
                    selectValue += chbELicense.Items[i].Value + ",";
                    mark++;
                }
            }
            if (mark == 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "员工权限不能设置为空!!");
                return;
            }
            theone.LICENSE_NAME = selectValue;

            m_df.Add(user.UpdateSQL(theone, CurrentId));
            if (m_df.Execute() == true)
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "修改用户成功!");
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "xxxe", "window.location.href='Admin.aspx';", true);
            }
            else
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "修改用户失败!");

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin.aspx");
        }

      
    }
}